﻿using AntdUI;
using Super.Admin.Global;
using Super.Admin.Utils.ThemeManager;
using Super.Admin.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Super.Admin.Utils.EventAggregator.EventType;
using Super.Admin.Utils.EventAggregator;
using Super.Admin.IServices;
using Super.Admin.Entities;
using Super.Admin.Services;
using Super.Admin.Models.User;
using Super.Admin.Mappers;
using Super.Admin.Models.Role;
using Super.Admin.Models.Department;
using System.Runtime.Intrinsics.Arm;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Super.Admin.Models.Permission;
using System.Security.Policy;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Super.Admin.Views.Page
{
    public partial class MenuManagementModify : UserControl
    {
        public List<PermissionBaseModel> _permissionModels { get; set; }
        public List<PermissionBaseModel> _allPermissionModels { get; set; }
        private PermissionBaseModel _currentPermissionModel;
        private string _title;
        private Form _form;
        private bool _isCreateChild;
        public MenuManagementModify(Form form, List<PermissionBaseModel> permissionModels, List<PermissionBaseModel> allPermissionModels, PermissionBaseModel currentModel, string title)
        {
            InitializeComponent();
            _form = form;
            _permissionModels = permissionModels;
            _allPermissionModels = allPermissionModels;
            _currentPermissionModel = currentModel;
            _title = title;
            this.Load += MenuManagementModify_Load;
        }

        private void MenuManagementModify_Load(object? sender, EventArgs e)
        {
            InitializeControls();
        }

        private void InitializeControls()
        {
            _isCreateChild = false;
            LblTitle.Text = _title;
            BtnSwitchMneuType.Items.Clear();
            if (_title.Contains("新增菜单"))
            {
                BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "0", Text = "目录" });
                BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "1", Text = "菜单" });
                BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "2", Text = "按钮" });
                BtnSwitchMneuType.SelectIndex = 0;
                BtnSwitchMneuType.Enabled = true;
            }
            else if (_title.Contains("编辑"))
            {
                if (_currentPermissionModel.Type == 0)
                {
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "0", Text = "目录" });
                }
                else if (_currentPermissionModel.Type == 1)
                {
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "1", Text = "菜单" });
                }
                else if (_currentPermissionModel.Type == 2)
                {
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "2", Text = "按钮" });
                }
                BtnSwitchMneuType.SelectIndex = 0;
                BtnSwitchMneuType.Enabled = false;
            }
            else if (_title.Contains("新增子菜单"))
            {
                _isCreateChild = true;
                if (_currentPermissionModel.PermissionParent.Type == 0)
                {
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "0", Text = "目录" });
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "1", Text = "菜单" });
                }
                else if (_currentPermissionModel.PermissionParent.Type == 1)
                {
                    BtnSwitchMneuType.Items.Add(new SegmentedItem() { ID = "2", Text = "按钮" });
                }
                BtnSwitchMneuType.SelectIndex = 0;
                BtnSwitchMneuType.Enabled = true;
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void BtnSwitchMneuType_SelectIndexChanged(object sender, IntEventArgs e)
        {
            PnlMenuType.Controls.Clear();
            _currentPermissionModel.Type = int.Parse(BtnSwitchMneuType.Items[BtnSwitchMneuType.SelectIndex].ID);
            // 添加根节点
            _permissionModels = new List<PermissionBaseModel>
                {
                    new PermissionBaseModel
                    {
                        Id = 0,
                        Name = "Root",
                        PermissionChildren = _permissionModels
                    }
                };
            if (BtnSwitchMneuType.Items[BtnSwitchMneuType.SelectIndex].ID == "0")
            {
                PnlMenuType.Controls.Add(new MenuManagementModifyCatalogue(_form, _permissionModels, _allPermissionModels, _currentPermissionModel, _isCreateChild));
            }
            else if (BtnSwitchMneuType.Items[BtnSwitchMneuType.SelectIndex].ID == "1")
            {
                PnlMenuType.Controls.Add(new MenuManagementModifyMenu(_form, _permissionModels, _allPermissionModels, _currentPermissionModel, _isCreateChild));
            }
            else if (BtnSwitchMneuType.Items[BtnSwitchMneuType.SelectIndex].ID == "2")
            {
                PnlMenuType.Controls.Add(new MenuManagementModifyButton(_form, _permissionModels, _allPermissionModels, _currentPermissionModel, _isCreateChild));
            }
        }
    }
}